//
//  WBVisitorView.swift
//  微博Demo
//
//  Created by 花化华 on 2016/11/22.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class WBVisitorView: UIView {

    // MARK: - 构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: 注册登录按钮
    //注册按钮
    lazy var registerButton: UIButton = UIButton.cz_textButton(
        "注册",
        fontSize: 14,
        normalColor: UIColor.darkGray,
        highlightedColor: UIColor.black,
        backgroundImageName: "common_button_white_disable")
    
    //登录按钮
    lazy var loginButton: UIButton = UIButton.cz_textButton(
        "登录",
        fontSize: 14,
        normalColor: UIColor.orange,
        highlightedColor: UIColor.black,
        backgroundImageName: "common_button_white_disable")
    
    
    // MARK: - 私有控件
    /// 懒加载属性只有调用 UIKit 控件的指定构造函数，其他都需要使用类型
    /// 大图标
    fileprivate lazy var iconImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    /// 菊花
    fileprivate lazy var cirelImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    /// 遮罩图像 - 不要使用 maskView
    fileprivate lazy var maskImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    /// 提示标签
    fileprivate lazy var textLabel: UILabel = UILabel.cz_label(withText: "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜", fontSize: 14, color: UIColor.darkGray)
    
    /// 旋转图标动画（首页）
    fileprivate func startAnimation() {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = 2*M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 15
        
        // 动画完成不删除，如果 iconView 被释放，动画会一起销毁！
        // 在设置连续播放的动画非常有用！
        anim.isRemovedOnCompletion = false
        
        // 将动画添加到图层
        cirelImageView.layer.add(anim, forKey: nil)
    }
    
    /// 访客视图的信息字典 [imageName / message]
    /// 如果是首页 imageName == ""
    var visitorInfo: [String: String]? {
        didSet {
            // 1> 取字典信息
            guard let imageName = visitorInfo?["imageName"],
                    let message = visitorInfo?["message"] else {
                return
            }
            
            // 2> 设置消息
            textLabel.text = message
            
            // 3> 设置图像，首页不需要设置
            if imageName == "" {
                startAnimation()
                return
            }
            
            iconImageView.image = UIImage(named: imageName)
            
            // 其他控制器的访客视图不需要显示小房子/遮罩视图
            cirelImageView.isHidden = true
            maskImageView.isHidden = true
        }
    }
    
}

// MARK: - 设置界面
extension WBVisitorView {
    
    fileprivate func setupUI() {
        //自动布局的原则: 从上到下,从左到右,依次布局,如果控件较多,每布局完成一个, 就跑一下看看效果
        // 0. 在开发的时候，如果能够使用颜色，就不要使用图像，效率会更高！
        backgroundColor = UIColor.cz_color(withHex: 0xEDEDED)
        
        // 1. 添加控件
        addSubview(cirelImageView)
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(textLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        // 文本居中
        textLabel.textAlignment = .center

        // 2. 自动布局
        let margin: CGFloat = 20.0

        iconImageView.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-60)
        }
        
        cirelImageView.snp.makeConstraints { (make) in
            make.center.equalTo(iconImageView)
        }
        
        maskImageView.snp.makeConstraints { (make) in
            make.left.right.equalTo(self)
            make.bottom.equalTo(cirelImageView)
        }
        
        textLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.top.equalTo(cirelImageView.snp.bottom).offset(margin)
            make.left.equalTo(self).offset(80)
            make.right.equalTo(self).offset(-80)
        }
        
        registerButton.snp.makeConstraints { (make) in
            make.left.equalTo(self).offset(80)
            make.top.equalTo(textLabel.snp.bottom).offset(margin)
            make.size.equalTo(CGSize(width: 86, height: 35))
        }
        
        loginButton.snp.makeConstraints { (make) in
            make.size.equalTo(registerButton)
            make.bottom.equalTo(registerButton)
            make.right.equalTo(self).offset(-80)
        }
        
    }
    
}
